﻿using System;
using System.Linq;
using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;

namespace CardPlay.DemoPack.Games.BeleaguredCastleVariants
{
    [Game("285F6207-2745-46BB-AF39-13D0508159F6", LevelOfDifficulty.Medium, LevelOfSkill.MostlySkill, DurationOfPlay.Within5To10Minutes, Family = FamilyName)]
    public class Citadel : BeleagueredCastle
    {
        private class DealTableauAction : IDealAction
        {
            public DealTableauAction(Func<Card, string> targetSelector)
            {
                SelectTarget = targetSelector;
            }

            private Func<Card, string> SelectTarget { get; set; }

            public bool Execute(IDealContext context)
            {
                for (var i = 0; i < 6; ++i)
                {
                    context.DealCard(CardFaceSpecification.Any, CardOrientation.FaceUp, SelectTarget);                                        
                }

                return true;
            }
        }

        protected override IDealAction CreateDealScript()
        {
            var script = new DealScript();
            script.DealAcesTo(NamesOfFoundations);

            var sequence = new DealSequence();
            sequence.Add(script.Compile());

            foreach (var pile in PilesNamed(NamesOfTableaus))
            {
                var targetPile = pile;
                sequence.Add(new DealTableauAction(card =>
                    PilesNamed(NamesOfFoundations)
                        .Where(foundation => FoundationBuildSequence.IsSatisfiedBy(foundation.TopCard, card))
                        .Select(foundation => foundation.Name)
                        .Concat(new[] { targetPile.Name })
                        .First()));
            }

            return sequence;
        }
    }
}